﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenPop.Pop3;
using OpenPop.Mime;
using System.Text.RegularExpressions;

namespace OutlookInvitationTest1
{
    public partial class RecieveOutlookResponses2 : System.Web.UI.Page
    {
        List<string> invitationResponses = new List<string>();
        //

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //pobierze maile
        protected void Button1_Click(object sender, EventArgs e)
        {
            // The client disconnects from the server when being disposed
            using (Pop3Client client = new Pop3Client())
            {
                    // Connect to the server
                    client.Connect(TextBoxPOPHost.Text, int.Parse(TextBoxPort.Text), CheckBoxSSL.Checked);

                    // Authenticate ourselves towards the server
                    client.Authenticate(TextBoxEmail.Text, TextBoxPassword.Text);

                    // Get the number of messages in the inbox
                    int messageCount = client.GetMessageCount();

                    // We want to download all messages
                    List<Message> allMessages = new List<Message>(messageCount);

                    // Messages are numbered in the interval: [1, messageCount]
                    // Ergo: message numbers are 1-based.
                    // Most servers give the latest message the highest number
                    for (int i = messageCount; i > 0; i--)
                    {
                        allMessages.Add(client.GetMessage(i));
                    }
                    List<String> rawTextMessages = new List<String>(messageCount);
                    foreach (Message item in allMessages)
                    {
                        String textVersion;
                        List<MessagePart> textVersions = item.FindAllTextVersions();
                        if (textVersions.Count >= 1)
                            textVersion = textVersions[0].GetBodyAsText();
                        else
                            textVersion = "<<OpenPop>> Cannot find a text version body in this message to show <<OpenPop>>";

                        rawTextMessages.Add(textVersion);
                        LabelStatus1.Text = "Sciagnieto wiaadomosci: " + messageCount;
                    }
                    int counter=0;
                    foreach (String s in rawTextMessages)
                    {
                        counter++;
                        TextBox1.Text +="---WIADOMOŚĆ: "+counter+" ---";
                        TextBox1.Text += s + "\n\n";
                        HiddenLabel.Text += s + ";:;";
                    }
                    client.Disconnect();
            }
        }

        //sparsuj maile
        protected void Button2_Click(object sender, EventArgs e)
        {
            OutlookMeetingParser outlookMeetingParser = new OutlookMeetingParser();
            List<AcceptanceMessage> acceptanceMessages = new List<AcceptanceMessage>();
            String rawString = HiddenLabel.Text;
            String[] rawTextMessages = Regex.Split(rawString, ";:;");
            List<String> rawTextMessagesList = new List<string>(rawTextMessages);
            if (rawTextMessages!=null)
            {
                acceptanceMessages = outlookMeetingParser.parseMessages(rawTextMessagesList);
                foreach(AcceptanceMessage acceptanceMessage in acceptanceMessages){
                    LabelStatus2.Text += "Iniciatywa ID: " + acceptanceMessage.getUID()+" ";
                    if (acceptanceMessage.isAccepted())
                    {
                        LabelStatus2.Text += "Zaakceptowana ";
                    }
                    else {
                        LabelStatus2.Text += "Niezaakceptowana ";
                    }
                    LabelStatus2.Text += "przez użytkownika: " + acceptanceMessage.getAttendeeEmail()+"\n";
                }
            }
            else {
                LabelStatus2.Text = "Brak wiadomosci do sparsowania!";
            }

        }

    }
}